<template>
  <!-- 查看详情 2018/3/7 11:44 zhangQ -->
  <div>
    <div>
      <Row>
        <i-col span="4">
          <Button type="primary" @click="dispatchMode=!dispatchMode" v-if="Boolean(!currentMainOrder.assignmentTime)">{{ dispatchMode?'已派单列表':'派单列表' }}</Button>
          <Button type="error" v-show="dispatchMode" @click="dispatchOrderToFleet" v-if="Boolean(!currentMainOrder.assignmentTime)">派单</Button>
        </i-col>
      </Row>
      <div v-show="dispatchMode">
        <Table ref="subOrderTab" :columns="subOrderColumn" border :data="subOrderData" :height="subOrderTabHeight" @on-selection-change="subOrderTabSelectChange"></Table>
      </div>
      <div style="margin-top: 15px;">
        <dispatchedList v-if="!dispatchMode" :currentMainOrder="currentMainOrder"></dispatchedList>
      </div>

      <!-- 选择车队，要派单了 -->
      <xlinkModal v-model="operatModal.dispatchModalShow" className="vertical-center-modal" title="分组派单" width="700">
        <dispatchToFleet v-if="operatModal.dispatchModalShow" @close="operatModal.dispatchModalShow = false" @assignment="assignmentToFleetForGrpoup"></dispatchToFleet>
      </xlinkModal>

    </div>
  </div>
</template>

<script>
  import {getFleetOrderDetailList, assignmentToSubFleet} from '@api/backEnd/mainOrder'
  import tablePage from '@components/table/tablePage.vue'
  import dispatchedList from './dispatchedList'
  import {mapGetters} from 'vuex'
  import _ from 'lodash'
  import xlinkModal from '@components/modal/xlinkModal'
  import dispatchToFleet from './dispatchToFleet'
  import {getTrainTypeCN} from './fleetDispatch/components/trainTypeEnum'

  export default {
    name: 'view-detail',
    data () {
      let subOrderColumn = [
        {
          type: 'index',
          width: 60,
          fixed: 'left'
        },
        {
          type: 'selection',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        {
          title: '联运子运单号',
          key: 'subOrderId',
          width: 200,
          fixed: 'left'
        },
        {
          title: '货票号',
          key: 'waybillNo',
          width: 100,
          fixed: 'left'
        },
        {
          title: '火车车种类型',
          ellipsis: 'false',
          width: 150,
          render: (h, params) => {
            let trainType = params.row.trainType
            let trainTypeCN = getTrainTypeCN(trainType)
            return (<span>{trainTypeCN}</span>)
          }
        },
        {
          title: '箱号',
          key: 'containerNo',
          width: 85
        },
        {
          title: '业务类型',
          key: 'transportType',
          width: 150,
          render: (h, params) => {
            let transportType = params.row.transportType
            let transportTypeW = ''
            if (transportType === 1) {
              transportTypeW = '门到门'
            } else if (transportType === 2) {
              transportTypeW = '门到站'
            } else if (transportType === 3) {
              transportTypeW = '站到门'
            } else if (transportType === 4) {
              transportTypeW = '站到站'
            }
            return (<span>{transportTypeW}</span>)
          }
        },
        {
          title: '货票重量',
          key: 'weight',
          width: 100
        },
        {
          title: '收货人',
          key: 'receiver',
          width: 250
        },
        {
          title: '发货人',
          key: 'shipper',
          width: 250
        },
        {
          title: '接取费',
          key: 'receiveFee',
          width: 100
        },
        {
          title: '品名',
          key: 'pm',
          width: 100
        }
      ]
      let subOrderData = []
      let positionList = []
      let noticeRecordData = []
      let subOrderTabHeight = 550
      let pageSizeOpts = [150, 100, 50]
      let currentCheckeds = []
      let dispatchMode = true
      let operatModal = {dispatchModalShow: false}
      let orderState = [
        {'state': '正常', val: '1'},
        {'state': '已派单', val: '2'}
      ]
      let datas = {orderState, subOrderColumn, subOrderData, positionList, noticeRecordData, subOrderTabHeight, pageSizeOpts, operatModal, currentCheckeds, dispatchMode}
      return datas
    },
    props: {
      currentMainOrder: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    computed: {
      ...mapGetters(['currentUser', 'activeMenu'])
    },
    methods: {
      /**
       * TODO(场站派单给车队)
       * @auther zhangQ
       * @date 2018/6/12 14:53
       */
      dispatchOrderToFleet () {
        if (this.currentCheckeds.length > 0) {
          this.operatModal.dispatchModalShow = true
        } else {
          this.$Notice.warning({
            title: '警告',
            desc: '请选择子运单'
          })
        }
      },
      /**
       * TODO(向服务器发送)
       * @auther zhangQ
       * @date 2018/6/13 7:42
       */
      assignmentToFleetForGrpoup (assignmentForm, fleetList) {
        console.log('LogisticsMonitoring')
        console.log('assignmentToFleetForGrpoup (assignmentForm, fleetList)')
        let assignmentFormClone = _.cloneDeep(assignmentForm)
        if (!(assignmentFormClone.checkTrackName.length > 0)) {
          this.$Notice.warning({
            title: '错误',
            desc: '车队必选'
          })
          return
        }
        let maps = []
        assignmentFormClone.checkTrackName.forEach((item) => {
          let fleetName = _.find(fleetList, (fleetNameItem) => {
            return fleetNameItem.id === item
          })
          let mapsDeatil = []
          this.currentCheckeds.forEach((subOrderItem) => {
            let subOrderObj = {
              id: subOrderItem.id,
              fleetOrderId: this.currentMainOrder.id,
              subOrderId: subOrderItem.subOrderId,
              waybillId: subOrderItem.waybillId,
              waybillNo: subOrderItem.waybillNo,
              weight: subOrderItem.weight,
              pm: subOrderItem.pm,
              shipper: subOrderItem.shipper,
              receiver: subOrderItem.receiver,
              outContacts: assignmentFormClone.contacts,
              userAddressId: assignmentFormClone.userAddressId,
              outMobile: assignmentFormClone.contactsMobile,
              outAddress: assignmentFormClone.address,
              kilometreNum: assignmentFormClone.kilometreNumber,
              containerNo: subOrderItem.containerNoHead + subOrderItem.containerNoBody,
              containerHead: subOrderItem.containerNoHead,
              containerBody: subOrderItem.containerNoBody,
              trainType: subOrderItem.trainType,
              transportType: subOrderItem.transportType,
              receiveFee: subOrderItem.zkjf017,
              deliverFee: subOrderItem.zkjf018,
              orgId: fleetName.id,
              orgName: fleetName.login_name,
              isTotalSend: 1
            }
            mapsDeatil.push(subOrderObj)
          })
          let obj = {
            id: this.currentMainOrder.id,
            dispatchOrgId: this.currentUser.userId,
            dispatchOrgName: this.currentUser.loginName,
            orgId: fleetName.id,
            orgName: fleetName.login_name,
            dispatchType: 2, // 分组派单
            mainOrderNo: this.currentMainOrder.mainOrderNo,
            startStationCode: this.currentMainOrder.startStationCode,
            startStationName: this.currentMainOrder.startStationName,
            endStationCode: this.currentMainOrder.endStationCode,
            endStationName: this.currentMainOrder.endStationName,
            isContainer: this.currentMainOrder.isContainer,
            paraPositionTrack: assignmentFormClone.paraPositionTrack,
            estimateArriveTime: assignmentFormClone.estimateArriveTime,
            paraPositionTime: assignmentFormClone.estimateArriveTime,
            xlFleetOrderDetails: mapsDeatil,
            isTotalSend: 1
          }
          maps.push(obj)
        })
        // 开始发送
        let xlFleetOrderList = maps
        let param = {xlFleetOrderList}
        console.log(param)
        assignmentToSubFleet(param).then(res => {
          if (+res.data.isSuccess === 1) {
            this.$Notice.success({
              title: '成功',
              desc: '派单成功'
            })
            this.getSubOrderData()
            this.currentCheckeds = []
            this.operatModal.dispatchModalShow = false
            this.$emit('dispatchTofleetDataRefresh')
          } else {
            this.$Notice.error({
              title: '警告',
              desc: '派单失败'
            })
          }
        })
      },
      subOrderTabSelectChange (rows) {
        this.currentCheckeds = rows
      },
      /**
       * TODO(获得子运单信息)
       * @auther zhangQ
       * @date 2018/3/16 15:40
       **/
      getSubOrderData () {
        if (!(this.currentMainOrder && this.currentMainOrder.id)) {
          return
        }
        let param = {
          fleetOrderId: this.currentMainOrder.id,
          stateGroup: '1'
        }
        getFleetOrderDetailList(param).then(res => {
          if (res.data.isSuccess === 1) {
            this.subOrderData = res.data.data
            if (this.subOrderData && this.subOrderData > 0) {
              this.subOrderTabHeight = 500
            }
          } else {
            Promise.reject(new Error(res.data.res))
          }
        }).catch((err) => {
          console.log('接口getOrderDetail:' + err)
        })
      }
    },
    mounted () {
      this.getSubOrderData()
    },
    components: {tablePage, xlinkModal, dispatchToFleet, dispatchedList}
  }
</script>
